<?php
class cls_serve_order extends cls_serve_base
{
	function get_cart_info($product_list_with_num)
	{
		$product_list = array();
		foreach ($product_list_with_num as $product) {
			$product_list[] = $product['id'];
		}
		$data = parent::get_data();
		$product_info = $data->get_product_info($product_list);

		foreach ($product_list_with_num as $product) {
			$product_info[$product['id']]['Amount'] = $product['num'];
			$product_info[$product['id']]['current_price'] = $product['price'];
		}
		return $product_info;
	}
	
	function check_cart_valid($product_list_with_num, $product_info)
	{
		$error_info = array();
		foreach ($product_list_with_num as $k => $product)
		{
			if (!isset($product_info[$product['id']])) {
				$error_info[] = array(
						'type' => 'Not Exist',
						'id' => $product['id'],
					);
				continue;
			}
			if ($product['num'] > $product_info[$product['id']]['Quantity']) {
				$error_info[] = array(
						'type' => 'Stock Out',
						'id' => $product['id'],
					);
				continue;
			}
		}
		
		if (!empty($error_info)) {
			return return_info(false, $error_info);
		}
		return return_info();
	}
	
	function place_order($user_info, $product_list_with_num)
	{
		$product_info = $this->get_cart_info($product_list_with_num);
		
		$result = $this->check_cart_valid($product_list_with_num, $product_info);
		
		if (!$result['success']) {
			return $result;
		}
		
		return $this->_place_order($user_info, $product_list_with_num, $product_info);
	}
	
	function get_total_price($product_list_with_num, $product_info)
	{
		$total_price = 0;
		foreach ($product_list_with_num as $product)
		{
			$total_price += $product_info[$product['id']]['current_price'] * $product['num'];
		}
		return $total_price;
	}
	
	private function _place_order($user_info, $product_list_with_num, $product_info)
	{
		$total_price = $this->get_total_price($product_list_with_num, $product_info);
		$product_list = array();
		foreach ($product_list_with_num as $product) {
			$product_list[] = array(
					'ProductID' => $product['id'],
					'ProductName' => $product_info[$product['id']]['Name'],
					'Amount' => $product['num'],
					'TotalPrice' => $product_info[$product['id']]['current_price'] * $product['num'],
				);
		}
		$data = parent::get_data();
		$address_info = $data->get_Address_info($user_info['address']);
		if (empty($address_info) || $address_info['CustomerID'] != $_SESSION['user']['ID']) {
			return return_info(false, '收获地址异常');
		}
		$order = array(
				'CustomerID' => $user_info['user_id'],
				'OrderTime' => get_now(),
				'TotalPrice' => $total_price,
				'OrderState' => cls_config_order::$order_state['non_deliver'],
				'PaymentTypeID' => 0,
				'Receiver' => $address_info['Receiver'],
				'Phone' => $address_info['Phone'],
				'Postal' => $address_info['Postal'],
				'Address' => $address_info['Address']
			);
		$order_id = $data->place_order($order, $product_list);
		
		return return_info();
	}

	function get_order_list($page,$rp,$where,$sort)
	{
		$order_data = parent::get_data();
		$order_list = $order_data->get_order_list($page,$rp,$where,$sort);
		return $order_list;
	}

	
	function get_orderItem($pid)
	{
		$orderItem_data = parent::get_data();
		$orderItem_list = $orderItem_data->get_orderItem($pid);
		return $orderItem_list;
	} 
	
	function getCount($where)
	{
		$order_data = parent::get_data();
		$count = $order_data->countRec('id',$where);
		return $count;
	}
	
	
	function modify_order($items)
	{
		$order_data = parent::get_data();
		$order_data->modify_order($items);
		
	}
}